package com.hanlink.dsp.protocols;

import android.util.Log;

import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.DspSettings;
import com.hanlink.dsp.ca.model.channel.XoverChannel;
import com.qztech.bluetooth.BluetoothClientManager;
import com.qztech.bluetooth.util.ByteUtil;

/**
 * @author Zhanghb
 * Email: 2573475062@qq.com
 * Date : 2019-09-22 13:02
 */
public class XoverProtocol {
    //1 高通 2 低通 3高通bypass 4 低通bypass   5 bandpass
    //0 Bessel
    //1 Butter-w
    //2 Link-ril
    public static final int XOVER_FILTER_TYPE_BESSEL    = 0;
    public static final int XOVER_FILTER_TYPE_BUTTER_W  = 1;
    public static final int XOVER_FILTER_TYPE_LINK_RIL  = 2;

    public static final int PASS_TYPE_UNKNOWN  = 0;
    public static final int PASS_TYPE_HIGH_PASS = 1;
    public static final int PASS_TYPE_LOW_PASS = 2;
    public static final int PASS_TYPE_HIGH_BYPASS = 3;
    public static final int PASS_TYPE_LOW_BYPASS = 4;
    public static final int PASS_TYPE_BAND_PASS = 5;
    public static final int PASS_TYPE_BAND_BPASS = 6;


    public static void send(XoverChannel xoverChannel, int passType, boolean withGroup) {

//        {
//                (byte) (0xAA & 0xff),
//                (byte) (0x55 & 0xff),
//                (byte) (0xDD & 0xff),//1
//                0,//2
//                0,//3
//                0,//4.
//                0,//5.
//                0,//6.
//                0,//7.
//                0,//8.
//                0,//9.
//                0,//10
//                0,//11
//                0,//12
//                0//13
//        };
        final int curChannel = xoverChannel.getChannel();
        int[] channels = DspSettings.XOVER_SETTINGS.getGroupChannel(curChannel);
        if (channels != null){
            byte[] buffer1 = getCmd(xoverChannel, passType, (byte)(0xC1 + curChannel));
            BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer1);
            Log.d("xover", ByteUtil.bytesToHexString(buffer1)+"  "+ passType );
            for (int channel : channels) {
                if (curChannel == channel)
                    continue;
                buffer1 = getCmd(xoverChannel, passType, (byte)(0xC1 + channel));
                Log.d("xover", ByteUtil.bytesToHexString(buffer1)+"  "+ passType );
                BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer1);
            }
        }

    }

    public static byte[] getCmd(XoverChannel xoverChannel, int passType, byte channel) {
        Log.d("xover" ," getcmd-:"+passType);
        boolean isHigh = passType == PASS_TYPE_HIGH_PASS || passType == PASS_TYPE_HIGH_BYPASS;
        boolean isHighByPass = (passType == PASS_TYPE_HIGH_BYPASS || passType ==PASS_TYPE_BAND_BPASS);
        boolean isLowByPass = (passType == PASS_TYPE_LOW_BYPASS || passType ==PASS_TYPE_BAND_BPASS);
        int i = 0;
        byte[] buffer = new byte[14];
        buffer[i++] = (byte) (0xAA & 0xff);
        buffer[i++] = (byte) (0x55 & 0xff);
        buffer[i++] = (byte) (isHigh ? 0xDD : 0xDE);
        buffer[i++] = channel;
        int filterType = (isHigh ? xoverChannel.getHighFilterType() : xoverChannel.getLowFilterType());
        int filterValue = (isHigh ? xoverChannel.getHighFilterValue() : xoverChannel.getLowFilterValue());

        switch (filterType){
            case XOVER_FILTER_TYPE_LINK_RIL:
                switch (filterValue){
                    case 2:
                    case 4:
                        filterValue = 0;
                        break;
                    case 3:
                        filterValue = 2;
                        break;
                    case 5:
                        filterValue = 3;
                        break;
                }
                break;
        }
        buffer[i++] = (byte) filterType; // 4
        buffer[i++] = (byte) filterValue; // 5
        if (isHighByPass) {
            buffer[i++] = (byte) (20 >> 8 & 0xff);
            buffer[i++] = (byte) (20 & 0xff);
        } else if (isLowByPass) {
            buffer[i++] = (byte) (20000 >> 8 & 0xff);
            buffer[i++] = (byte) (20000 & 0xff);
        } else {
            buffer[i++] = (byte) ((isHigh ? xoverChannel.getHighFrequency() : xoverChannel.getLowFrequency()) >> 8 & 0xff);
            buffer[i++] = (byte) ((isHigh ? xoverChannel.getHighFrequency() : xoverChannel.getLowFrequency()) & 0xff);
        }
        return buffer;
    }
}
